package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.ZorderDao;
import cn.gson.fengboot.model.dao.ZreturnedgoodDao;
import cn.gson.fengboot.model.dao.ZreturnedmoneyDao;
import cn.gson.fengboot.model.mapper.ZinvoiceMapper;
import cn.gson.fengboot.model.mapper.ZreturnedmoneyMapper;
import cn.gson.fengboot.model.pojos.Zinvoice;
import cn.gson.fengboot.model.pojos.Zorder;
import cn.gson.fengboot.model.pojos.Zreturnedgood;
import cn.gson.fengboot.model.pojos.Zreturnedmoney;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ZreturnedmoneyService {
    @Autowired
    ZreturnedmoneyMapper hkmapper;

    @Autowired
    ZreturnedmoneyDao dao;

    @Autowired
    ZorderDao dddao;

    @Autowired
    ZreturnedgoodDao thddao;

    //查询回款计划方法
    public List<Zreturnedmoney> hklist(String search,String search2,String search3){
        return hkmapper.hklist(search,search2,search3);
    }

    //新增页面按照客户id查询为回款的回款计划
    public List<Zreturnedmoney> hkjhlist(String search,String search2){
        return hkmapper.hkjhlist(search,search2);
    }

    //查询订单回款计划方法
    public List<Zreturnedmoney> ddhkjh(String search){
        return hkmapper.ddhkjh(search);
    }

    //查询订单退款计划方法
    public List<Zreturnedmoney> ddtkjh(String search){
        return hkmapper.ddtkjh(search);
    }

    //查询退货单退款计划方法
    public List<Zreturnedmoney> thdtkjh(String search){
        return hkmapper.thdtkjh(search);
    }

    //删除回款计划方法-物理删除
    public void sc2(Zreturnedmoney jh){
        Zorder zorder = dddao.findById(jh.getOrderId().getOrderId()).get();
        zorder.setOrderReturned(zorder.getOrderReturned().add(jh.getReturnedmoneyMoney()));
        dddao.save(zorder);
        dao.delete(jh);
    }

    //新增修改回款计划
    public void add(Zreturnedmoney jh,Double yuanjg){
        Zorder zorder = dddao.findById(jh.getOrderId().getOrderId()).get();
        Zreturnedgood returgoodId = thddao.findById(jh.getReturgoodId().getReturgoodId()).get();

        if(yuanjg!=0 && jh.getReturnedmoneyRefund().equals("回款")){
            Double jg = yuanjg-jh.getReturnedmoneyMoney().doubleValue();
            BigDecimal bigDecimal = BigDecimal.valueOf(jg);
            zorder.setOrderReturned(zorder.getOrderReturned().add(bigDecimal));
            double v = zorder.getOrderReturned().doubleValue();
            if(v==0){
                zorder.setOrderHkzt("已回款");
            }else {
                zorder.setOrderHkzt("回款中");
            }
            dddao.save(zorder);
        }else if(yuanjg==0 && jh.getReturnedmoneyRefund().equals("回款")){
            BigDecimal jg = zorder.getOrderReturned().subtract(jh.getReturnedmoneyMoney());
            zorder.setOrderReturned(jg);
            double v = jg.doubleValue();
            if(v==0){
                zorder.setOrderHkzt("已回款");
            }else {
                zorder.setOrderHkzt("回款中");
            }
            dddao.save(zorder);

        }

        if(yuanjg!=0 && jh.getReturnedmoneyRefund().equals("退款")){
            Double jg = yuanjg-jh.getReturnedmoneyMoney().doubleValue();
            BigDecimal bigDecimal = BigDecimal.valueOf(jg);
            returgoodId.setReturgoodReimburse(returgoodId.getReturgoodReimburse().add(bigDecimal));
            double v = returgoodId.getReturgoodReimburse().doubleValue();
            if(v==0){
                returgoodId.setReturgoodState("已退款");
            }else {
                returgoodId.setReturgoodState("退款中");
            }
            thddao.save(returgoodId);
        }else if(yuanjg==0 && jh.getReturnedmoneyRefund().equals("退款")){
            BigDecimal jg = returgoodId.getReturgoodReimburse().subtract(jh.getReturnedmoneyMoney());
            returgoodId.setReturgoodReimburse(jg);
            double v = jg.doubleValue();
            if(v==0){
                returgoodId.setReturgoodState("已退款");
            }else {
                returgoodId.setReturgoodState("退款中");
            }
            thddao.save(returgoodId);
        }
        dao.save(jh);
    }

    //客户id查询回款计划
    public List<Zreturnedmoney> clienthkjh(String search){
        return hkmapper.clienthkjh(search);
    }

    //谭方圆=>客户合并查询回款计划
    public List<Zreturnedmoney> tfyhuikuanlists(Integer id1,Integer id2){
        return hkmapper.tfyhuikuanlists(id1, id2);
    }
}
